//
//  ComAndDecomData.m
//  SendGzipData
//
//  Created by URANUS on 15/4/13.
//  Copyright (c) 2015年 URANUS. All rights reserved.
//

#import "ComAndDecomData.h"
#define SHOTSTRLENGTH 8

@implementation ComAndDecomData

+ (NSString *)compressData:(NSString *)info {
    NSData *data = [info dataUsingEncoding:NSUTF8StringEncoding];
    NSData *compressData = [GzipDataUtil gzipData:data];
    NSString *base64String = [compressData base64EncodedStringWithOptions:0];
    NSInteger dataLength = [base64String length];
    NSString *compressedString = [base64String substringWithRange:NSMakeRange(SHOTSTRLENGTH, dataLength-SHOTSTRLENGTH)];
    
    return compressedString;
}

+ (NSString *)decompressData:(NSString *)info {
    NSString *base64String = [@"H4sIAAAA" stringByAppendingString:info];
    NSData *data = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
    NSData *deCompressData = [GzipDataUtil unGzipData:data];
    NSString *deCompressString = [[NSString alloc] initWithData:deCompressData
                                                       encoding:NSUTF8StringEncoding];
    deCompressString = [deCompressString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return deCompressString;
}

+ (NSString *)replaceUnicode:(NSString *)unicodeStr
{
    
    NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u"withString:@"\\U"];
    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""];
    NSString *tempStr3 = [[@"\""stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    NSString *returnStr = [NSPropertyListSerialization propertyListWithData:tempData options:NSPropertyListImmutable format:NULL error:NULL];
    return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n"withString:@"\n"];
}


@end
